package com.pat.jrgwt.client;

import java.util.Date;
import java.util.HashMap;

import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.client.ui.CellPanel;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.pat.jrgwt.shared.MyConstants;

public class ArchivePerfPor3 extends Composite
{

    public final static int PerfUnlevelColumn = 2;
    public final static int LeverageAdaptionColumn = 3;
    public final static int YearPerfColumn = 4;
    public final static int SumColumn = 5;
    public final static int MaxPerfColumn = 6;
    public final static int DrawdownColumn = 7;
    public final static int MaxDdColumn = 8;
    public final static int PerfLevColumn = 9;
    public final static int YearPerfLevColumn = 10;
    public final static int SumLevColumn = 11;
    public final static int MaxDdLevColumn = 12;
    
    protected double[][] m_arCellValues = null;

    protected HashMap<Integer, double[]> m_arPerfByYear = new HashMap<Integer, double[]>();
    
    String[] m_arHeaders = new String[] { "Year", "Month", "Perf. (unlev., %)", "Leveraga adaption", "Year perf. (unlev., %)", "Sum (unlev. %)", "Max perf. (unlev. %)", "Drawdown (unlev. %)", "Max DD (unlev. %)", "Perf. (lev., %)", "Year perf. (lev., %)", "Sum (lev. %)", "Max DD (lev. %)" };
    
    private FlexTable m_table = new FlexTable();

    /**
     * First year to be displayed
     */
    protected int m_startYear = 1900 + new Date().getYear();
    private final int m_totalYears = 5;
    

    public ArchivePerfPor3()
    {
        m_arPerfByYear.put(2006, new double[] {-0.52, 8.16, -3.61, 1.52, 7.82, 2.12, -2.67, 1.52, 6.79, 4.35, -2.48, -0.82});
        m_arPerfByYear.put(2007, new double[] { 3.24, 4.55, -2.23, 9.82, 2.54, 2.30, 3.06, 8.33, 2.27, 4.11, -2.54, 8.25 });
        m_arPerfByYear.put(2008, new double[] { 10.79, 4.24, -4.23, 1.90, -0.13, 12.10, 1.61, 1.83, 11.75, 11.31, -0.93, 9.55 });
        
        m_arCellValues = new double[36][11];
        m_arCellValues = new double[][] {
                {-0.52, -0.52, 0, 102.55, 103.07, 0.5202, 0.5202, -0.52, 0, 102.55, 0.5202}
                , {8.16, 8.16, 0, 110.71, 110.71, 0.5202, 0.5202, 8.16, 0, 110.71, 0.5202}
                , { -3.61, -3.61, 0, 107.11, 110.71, 3.6055, 3.6055, -3.61, 0, 107.11, 3.6055}
                    , { 1.52, 1.52, 0, 108.62, 110.71, 3.6055, 3.6055, 1.52, 0, 108.62, 3.6055}
                    , { 7.82, 7.82, 0, 116.44, 116.44, 3.6055, 3.6055, 7.82, 0, 116.44, 3.6055}
                    , { 2.12, 2.12, 0, 118.56, 118.56, 3.6055, 3.6055, 2.12, 0, 118.56, 3.6055}
                    , { -2.67, -2.67, 0, 115.89, 118.56, 2.6714, 3.6055, -2.67, 0, 115.89, 3.6055}
                    , { 1.52, 1.52, 0, 117.41, 118.56, 2.6714, 3.6055, 1.52, 0, 117.41, 3.6055}
                    , { 6.79, 6.79, 0, 124.20, 124.20, 2.6714, 3.6055, 6.79, 0, 124.20, 3.6055}
                    , { 4.35, 4.35, 0, 128.55, 128.55, 2.6714, 3.6055, 4.35, 0, 128.55, 3.6055}
                    , { -2.48, -2.48, 0, 126.07, 128.55, 2.4803, 3.6055, -2.48, 0, 126.07, 3.6055}
                    , { -0.82, -0.82, 22.18, 125.25, 128.55, 3.3048, 3.6055, -0.82, 22.18, 125.25, 3.6055}
                    , { 3.24, 3.24, 0, 128.49, 128.55, 3.3048, 3.6055, 3.24, 0, 128.49, 3.6055}
                    , { 4.55, 4.55, 0, 133.03, 133.03, 3.3048, 3.6055, 4.55, 0, 133.03, 3.6055}
                    , { -2.23, -2.23, 0, 130.80, 133.03, 2.2308, 3.6055, -2.23, 0, 130.80, 3.6055}
                    , { 9.82, 9.82, 0, 140.62, 140.62, 2.2308, 3.6055, 9.82, 0, 140.62, 3.6055}
                    , { 2.54, 2.54, 0, 143.15, 143.15, 2.2308, 3.6055, 2.54, 0, 143.15, 3.6055}
                    , { 2.30, 2.30, 0, 145.46, 145.46, 2.2308, 3.6055, 2.30, 0, 145.46, 3.6055}
                    , { 3.06, 3.06, 0, 148.52, 148.52, 2.2308, 3.6055, 3.06, 0, 148.52, 3.6055}
                    , { 8.33, 8.33, 0, 156.85, 156.85, 2.2308, 3.6055, 8.33, 0, 156.85, 3.6055}
                    , { 2.27, 2.27, 0, 159.12, 159.12, 2.2308, 3.6055, 2.27, 0, 159.12, 3.6055}
                    , { 4.11, 4.11, 0, 163.23, 163.23, 2.2308, 3.6055, 4.11, 0, 163.23, 3.6055}
                    , { -2.54, -2.54, 0, 160.68, 163.23, 2.5448, 3.6055, -2.54, 0, 160.68, 3.6055}
                    , { 8.25, 8.25, 43.69, 168.94, 168.94, 2.5448, 3.6055, 8.25, 43.69, 168.94, 3.6055}
                    , { 10.79, 10.79    , 179.73, 179.73, 2.5448, 3.6055, 10.79 , 179.73, 3.6055}
                    , { 4.24, 4.24  , 183.97, 183.97, 2.5448, 3.6055, 4.24  , 183.97, 3.6055}
                    , { -4.23, -4.23    , 179.75, 183.97, 4.2285, 4.2285, -4.23 , 179.75, 4.2285}
                    , { 1.90, 1.90  , 181.64, 183.97, 4.2285, 4.2285, 1.90  , 181.64, 4.2285}
                    , { -0.13, -0.13    , 181.52, 183.97, 2.4563, 4.2285, -0.13 , 181.52, 4.2285}
                    , { 12.10, 12.10    , 193.62, 193.62, 2.4563, 4.2285, 12.10 , 193.62, 4.2285}
                    , { 1.61, 1.61  , 195.23, 195.23, 2.4563, 4.2285, 1.61  , 195.23, 4.2285}
                    , { 1.83, 1.83  , 197.06, 197.06, 2.4563, 4.2285, 1.83  , 197.06, 4.2285}
                    , { 11.75, 11.75    , 208.81, 208.81, 2.4563, 4.2285, 11.75 , 208.81, 4.2285}
                    , { 11.31, 11.31    , 220.12, 220.12, 2.4563, 4.2285, 11.31 , 220.12, 4.2285}
                    , { -0.93, -0.93    , 219.19, 220.12, 0.9260, 4.2285, -0.93 , 219.19, 4.2285}
                    , { 9.55, 9.55, 59.81, 228.75, 228.75, 0.9260, 4.2285, 9.55, 59.81, 228.75, 4.2285}

        };
        
        VerticalPanel p = new VerticalPanel();
        p.add(m_table);
        redraw();
        initWidget(p);
    }
    
    public void redraw()
    {
//        loadData();
        drawTable(m_totalYears);
        recalculateData();
    }

    public void recalculateData()
    {
        for( int row = 0; row < m_arCellValues.length; row++ )
        {
            for( int column = 0; column < m_arCellValues[row].length; column++ )
            {
                m_table.setText(row + 1, column + 2, "" + m_arCellValues[row][column]);
            }
        }
    }

    private void drawTable(int totalYears)
    {
        m_table.setBorderWidth(1);
        m_table.setCellPadding(3);
        m_table.setCellSpacing(2);

        for( int i = 0; i < m_arHeaders.length; i++ )
        {
            m_table.setText(0, i, m_arHeaders[i]);
            m_table.getCellFormatter().setStylePrimaryName(0, i, "jrCellHeader");
        }

        for (int i = totalYears; i >= 0; i--)
        {
            drawOneYearGrid(m_startYear - i, (totalYears - i) * 12 + 1);
        }
    }

    protected void drawOneYearGrid(int year, int startRow)
    {
        // Display year
        m_table.setText(startRow, 0, "" + year);
        m_table.getCellFormatter().setStyleName(startRow, 0, "cellBold");
        
        // Sum for 12 months
        double total = 0.0;
        NumberFormat nf = NumberFormat.getFormat("###,##0.##");
        for (int i = 0; i < 12; i++)
        {
            double[] arPerf = m_arPerfByYear.containsKey(year) ? m_arPerfByYear.get(year) : new double[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
            m_table.setText(startRow + i, 1, "" + (i+1));
            m_table.setText(startRow + i, 2, "" + nf.format(arPerf[i]));
            total += arPerf[i];
            
            // Format empty cells
            for (int j = 3; j < 12; j++)
            {
                m_table.setText(startRow + i, j, "");
            }
        }
        m_table.getCellFormatter().setStyleName(startRow + 11, 3, "cellBold");
        m_table.setText(startRow + 11, 3, nf.format(total));   
    }


    /**
     * Returns value by year and month (1-12). No exception if out of range
     */
    public double getPerfByDate(int year, int month)
    {
        double v = Double.MIN_VALUE;
        
        if( m_arPerfByYear.containsKey(year) )
        {
            if( (month > 0) && (month < 13) )
            {
                v = m_arPerfByYear.get(year)[month - 1];
            }else
            {
//                GWT.log("No value for archive perf month " + month + " in year " + year);
            }
        }else
        {
//            GWT.log("No for archive perf year " + year);
        }
        
        return v;
    }


}
